<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>lodash的differenceWith实现</title>
</head>

<body>


  <script>
    let arr1 = [
      { name: 'John', age: 18 },
      { name: 'Alex', age: 22 },
      { name: 'Joe', age: 12 },
      { name: 'Anna', age: 19 },
      { name: 'Lily', age: 14 },
      { name: 'Avril', age: 18 },
    ]
    let arr2 = [
      { name: 'Anna', age: 19 },
      { name: 'Avril', age: 18 },
      { name: 'Alex', age: 22 },
      { name: 'Bob', age: 24 }

    ]


    function deepCompare(a, b) {
      for (let aKey in a) {
        if (a[aKey] === b[aKey]) continue
        else return false
      }
      for (bKey in b) {
        if (b[bKey] === a[bKey]) continue
        else return false
      }
      return true
    }


    function differenceWith(a, b, f) {
      let res = [...a]
      for (let item1 in a) {
        for (let item2 in b) {
          if (f(a[item1], b[item2]) === true) {
            res[item1] = undefined;
            break
          }
        }
      }
      return res.filter(item => {
        return item != undefined
      });
    }

    console.log(differenceWith(arr1, arr2, deepCompare));


  </script>
</body>

</html>